package com.ms.p2p.controller.app;


import com.ms.common.bean.Message;
import com.ms.common.exception.NoticeException;
import com.ms.common.exception.ResourceNotFoundException;
import com.ms.common.utils.*;
import com.ms.p2p.Constants;
import com.ms.p2p.entity.Page;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.entity.system.AdminUser;
import com.ms.p2p.template.directive.FlashMessageDirective;
import com.ms.p2p.utils.DictionaryUtils;
import com.ms.p2p.utils.SpringUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;

import static com.ms.common.utils.WebErrors.ERROR_ATTR_NAME;

public class BaseController {

    protected static Logger logger = Logger.getLogger(BaseController.class);

    protected static final String FRONT_ERROR_PAGE = "/common/error";

    protected static final Message successMessage = Message.success("操作成功");
    protected static final Message errorMMessage = Message.error("操作失败");


    @InitBinder
    protected void initBinder(WebDataBinder binder) {

            // String类型转换，将所有传递进来的String进行HTML编码，防止XSS攻击
            binder.registerCustomEditor(String.class,
                    new PropertyEditorSupport() {
                        @Override
                        public void setAsText(String text) {
                            setValue(text == null ? null : StringEscapeUtils
                                    .escapeHtml4(text.trim()));
                        }

                        @Override
                        public String getAsText() {
                            Object value = getValue();
                            return value != null ? value.toString() : "";
                        }
                    });
            // Date 类型转换
            binder.registerCustomEditor(Date.class,
                    new PropertyEditorSupport() {
                        @Override
                        public void setAsText(String text) {
                            try {
                                setValue(MsDateUtils.parseDate(text, "yyyy-MM-dd HH:mm:ss"));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                        }

                    });
    }

    protected void addActionError(HttpServletRequest request, String message) {
        WebErrors errors = (WebErrors) request.getAttribute(ERROR_ATTR_NAME);
        if (errors == null) {
            errors = WebErrors.create();
            request.setAttribute(ERROR_ATTR_NAME, errors);
        }
        errors.addErrorString(message);
    }

    @ExceptionHandler
    public void exceptionHandler(Exception ex, HttpServletResponse response,
                                 HttpServletRequest request) throws IOException, ServletException {


            if (ex.getClass() == NoticeException.class) {
                this.addActionError(request, ex.getMessage());
            } else {
                ex.printStackTrace();
                this.addActionError(request, "出现未知错误");
                logger.error("系统错误", ex);
            }
            WebErrors errors = (WebErrors) request
                    .getAttribute(ERROR_ATTR_NAME);
            errors.showErrorApp(response);

    }
}
